package user

import (
	"context"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) SigninUser(ctx context.Context, req *v1.SigninUserReq) (res *v1.SigninUserRes, err error) {
	err = service.User().SignIn(ctx, model.UserSignInInput{
		Email: req.Email,
		Password: req.Password,
	})
	return
}

func (c *ControllerV1) SignOut(ctx context.Context, req *v1.SignOutReq) (res *v1.SignOutRes, err error) {
	err = service.User().SignOut(ctx)
	return
}

func (c *ControllerV1) RegCompany(ctx context.Context, req *v1.RegCompanyReq) (res *v1.RegCompanyRes, err error) {
	err = service.User().Create(ctx, model.UserCreateInput{
		Email: req.Email,
		Password: req.Password,
	})
	return
}

func (c *ControllerV1) RegUser(ctx context.Context, req *v1.RegUserReq) (res *v1.RegUserRes, err error) {
	err = service.User().Create(ctx, model.UserCreateInput{
		Email: req.Email,
		Password: req.Password,
	})
	return
}

func (c *ControllerV1) ResetPassword(ctx context.Context, req *v1.ResetReq) (res *v1.ResetRes, err error) {
	err = service.User().ResetPassword(ctx, req)
	return
}

func (c *ControllerV1) CheckPassport(ctx context.Context, req *v1.CheckPassportReq) (res *v1.CheckPassportRes, err error) {
	available, err := service.User().IsPassportAvailable(ctx, req.Email)
	if err != nil {
		return nil, err
	}
	if !available {
		return nil, gerror.Newf(`Email "%s" is already token by others`, req.Email)
	}
	return
}

func (c *ControllerV1) IsSignedIn(ctx context.Context, req *v1.IsSignedInReq) (res *v1.IsSignedInRes, err error) {
	res = &v1.IsSignedInRes{
		OK: service.User().IsSignedIn(ctx),
	}
	return
}

func (c *ControllerV1) Profile(ctx context.Context, req *v1.ProfileReq) (res *v1.ProfileRes, err error) {
	res = &v1.ProfileRes{
		service.User().GetProfile(ctx),
	}
	return
}


